package com.running.base.run.mq;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);

    public static final String EXCHANGE_NAME = "run-fudao-exchange";
    public static final String QUEUE_NAME = "run-fudao-queue";
    public static final String ROUTE_NAME = "run-fudao-routing";

    //创建交换机
    @Bean(EXCHANGE_NAME)
    public Exchange getExchange(){
        logger.info("初始化rabbitmq-交换机：{}", EXCHANGE_NAME);
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).build();
    }
    //创建队列
    @Bean(QUEUE_NAME)
    public Queue getQueue(){
        logger.info("初始化rabbitmq-队列：{}", QUEUE_NAME);
        return new Queue(QUEUE_NAME);
    }
    //交换机和队列绑定
    @Bean
    public Binding exchangeBindQueue(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange){
        logger.info("交换机和队列绑定，关系：{}", ROUTE_NAME);
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_NAME).noargs();
    }
}

